package com.example.demotest.repository;

import com.example.demotest.pojo.Address;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AddressRepository extends JpaRepository<Address, Integer> {
    List<Address> findByUserId(Integer userId);

    int countByUserId(Integer userId);

    @Modifying
    @Query("UPDATE Address a SET a.defaultFlag = false WHERE a.userId = :userId AND a.id <> :addressId")
    void clearOtherDefaultFlags(@Param("userId") Integer userId, @Param("addressId") Integer addressId);
}
